package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.regionCommand.AddRegionCommand;
import com.woniu.command.regionCommand.DeleteRegionCommand;
import com.woniu.command.regionCommand.UpdateRegionCommand;
import com.woniu.inlet.vo.RegionVo;
import com.woniu.outlet.po.RegionPo;
import com.woniu.service.RegionService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "地区管理")
@Slf4j
@RequestMapping("/region/")
public class RegionController {

    @Autowired
    private RegionService regionService;

    @GetMapping("/regions")
    @ApiOperation("查询地区")
    public ResponseResult<List<RegionVo>> findRegions() throws Exception{
        List<RegionPo>  regionPos =  regionService.findRegions();
        List<RegionVo> regionVos = new ArrayList<>();
        //对象的深拷贝
        for (RegionPo item : regionPos){
            RegionVo regionVo = new RegionVo();
            BeanUtils.copyProperties(item,regionVo);
            regionVos.add(regionVo);
        }
        return new ResponseResult<>(regionVos, "查询成功", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/regions/id")
    @ApiOperation("根据id查询地区")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<RegionVo> getRegionById(Long id) throws Exception {
        RegionPo regionPo = regionService.getRegionById(id);

        //对象的深拷贝

        RegionVo regionVo = new RegionVo();
        BeanUtils.copyProperties(regionPo, regionVo);


        return new ResponseResult<>(regionVo,"查询成功", ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/add")
    @ApiOperation("添加地区")
    @ApiOperationSupport(ignoreParameters = {"id","spare1","spare2","spare3"})
    public ResponseResult<String> addRegion(RegionPo regionPo){
        Command command = new AddRegionCommand();
        command.execute(regionPo);
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"添加成功",ResultCode.SUCCESS.getCode());    }

    @PostMapping("/update")
    @ApiOperation("修改地区")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<String> updateRegion(RegionPo regionPo){
        Command command = new UpdateRegionCommand();
        command.execute(regionPo);
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除地区")
    public ResponseResult<String> deleteRegion(Long id){
        Command command = new DeleteRegionCommand();
        command.execute(id);
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
